import React from 'react'
import {AppRegistry, Alert, YellowBox } from 'react-native'
import {setJSExceptionHandler} from 'react-native-exception-handler';
import dva from './utils/dva'
import Router, { routerMiddleware } from './router'

import statisticsModel from './models/statistics/statistics'
import appModel from './models/app'
import homeModel from './models/home/home'
import rateanalyzeModel from './models/home/rateanalyze'
import assetLiabilityModel from './models/home/assetliability'
import profitModel from './models/home/profit'
import cashflowModel from './models/home/cashflow'
import routerModel from './models/router'
import newsModel from './models/news'
import forecastModel from './models/forecast/forecast'
import tzycModel from './models/forecast/tzyc'
import consultantModel from './models/consultant/consultant'

const app = dva({
  initialState: {},
  models: [
    statisticsModel,
    appModel,
    homeModel,
    routerModel,
    rateanalyzeModel,
    assetLiabilityModel,
    profitModel,
    cashflowModel,
    newsModel,
    forecastModel,
    tzycModel,
    consultantModel
  ],
  onAction: [routerMiddleware],
  onError(e) {
    console.log('onError', e)
  },
})


const errorHandler = (e, isFatal) => {
  if (isFatal) {
    Alert.alert(
      'Unexpected error occurred',
      `
        Error: ${(isFatal) ? 'Fatal:' : ''} ${e.name} ${e.message}
        Please close it.
        `,
      []
    )
  } else {
    Alert.alert(
      'Unexpected error occurred',
      `
        Error: ${(isFatal) ? 'Fatal:' : ''} ${e.name} ${e.message}
        Please close it.
        `,
      []
    )
    console.log(e) // So that we can see it in the ADB logs in case of Android if needed
  }
}

setJSExceptionHandler(errorHandler)


YellowBox.ignoreWarnings(['Warning: isMounted(...) is deprecated', 'Module RCTImageLoader']);

const App = app.start(<Router />)


AppRegistry.registerComponent('DvaStarter', () => App)
